package org.example.spring.service.userServiceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.spring.domain.dto.FlowerDTO;
import org.example.spring.mapper.FlowerMapper;
import org.example.spring.domain.pojo.Result;
import org.example.spring.domain.pojo.Flower;
import org.example.spring.service.FlowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class FlowerServiceImpl implements FlowerService {

    @Autowired
    private FlowerMapper flowerMapper;


    @Override
    public ArrayList<Flower> getAllFlower() {
        ArrayList<Flower> flowers = flowerMapper.getAllFlower();
        return flowers;
    }

    @Override
    public ArrayList<Flower> getPageFlower(FlowerDTO flowerPageDTO) {
        PageHelper.startPage(flowerPageDTO.getPage(), flowerPageDTO.getPageSize());
        Page<Flower> page = flowerMapper.getPageFlower(flowerPageDTO);
        ArrayList<Flower> flowers = new ArrayList<>();
        for (Flower flower : page) {
            flowers.add(flower);
        }
        return flowers;
    }

    @Override
    public void deleteFlowerById(int id) {
        flowerMapper.deleteFlowerById(id);
    }

    @Override
    public void updateFlower(Flower flower) {
        flowerMapper.updateFlower(flower);
    }

    @Override
    public void addFlower(Flower flower) {
        flowerMapper.addFlower(flower);
    }

    @Override
    public Result<ArrayList<String>> getFlowerImageBanner() {
        ArrayList<String> flowerImage = flowerMapper.getFlowerImage();
        ArrayList<String>resImg = new ArrayList<>();
        //随机获取5张图片 不重复
        for (int i = 0; i < 5; i++) {
            int randomIndex = (int) (Math.random() * flowerImage.size());
            resImg.add(flowerImage.get(randomIndex));
            flowerImage.remove(randomIndex);
        }
        return Result.success(resImg);
    }

    @Override
    public Result<ArrayList<Flower>> getHotFlower() {
        ArrayList<Flower> hotFlower = flowerMapper.getHotFlower();
        return Result.success(hotFlower);
    }

    @Override
    public Result<ArrayList<Flower>> getNewFlower() {
        ArrayList<Flower> newFlower = flowerMapper.getAllFlower();
        ArrayList<Flower> resFlower = new ArrayList<>();
        //随机获取4个新品花卉 不重复
        for (int i = 0; i < 4; i++) {
            int randomIndex = (int) (Math.random() * newFlower.size());
            resFlower.add(newFlower.get(randomIndex));
            newFlower.remove(randomIndex);
        }
        return Result.success(resFlower);
    }

    @Override
    public Flower getFlowerById(Integer id) {
        Flower flowers = flowerMapper.getFlowerById(id);
        return flowers;
    }

    @Override
    public int getTotalFlower(FlowerDTO flowerPageDTO) {
        int total = flowerMapper.getTotalFlower(flowerPageDTO);
        return total;
    }


}
